<html>
<head><meta charset="utf-8"><title>float ⬌ int conversions · project-portable-simd · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/index.html">project-portable-simd</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html">float ⬌ int conversions</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234540653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234540653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234540653">(Apr 14 2021 at 17:29)</a>:</h4>
<p>what's the semantics for our <code>f32 as i32</code> casts, again?</p>



<a name="234540664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234540664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234540664">(Apr 14 2021 at 17:29)</a>:</h4>
<p>Didn't I already make <code>to_int_unchecked</code>?</p>



<a name="234540681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234540681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234540681">(Apr 14 2021 at 17:30)</a>:</h4>
<p>Good question</p>



<a name="234540904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234540904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234540904">(Apr 14 2021 at 17:31)</a>:</h4>
<p>I would <em>like</em> to be able to overload the <code>as</code> operator, tbh.</p>



<a name="234540970"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234540970" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234540970">(Apr 14 2021 at 17:31)</a>:</h4>
<p>I think the general concensus is that <code>as</code> was a mistake for floats lol</p>



<a name="234541065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234541065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234541065">(Apr 14 2021 at 17:32)</a>:</h4>
<p>Precisely because the semantics are so unclear</p>



<a name="234541330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234541330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234541330">(Apr 14 2021 at 17:33)</a>:</h4>
<p>I know it's saturating.</p>



<a name="234541546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234541546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234541546">(Apr 14 2021 at 17:35)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="c1">// Since Rust 1.45, the `as` keyword performs a *saturating cast* when casting from float to int.</span>
<span class="w">    </span><span class="c1">// If the floating point value exceeds the upper bound or is less than the lower bound, the returned value will be equal to the bound crossed.</span>

<span class="w">    </span><span class="c1">// 300.0 is 255</span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"300.0 is {}"</span><span class="p">,</span><span class="w"> </span><span class="mf">300.0_</span><span class="k">f32</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u8</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="c1">// -100.0 as u8 is 0</span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"-100.0 as u8 is {}"</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">100.0_</span><span class="k">f32</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u8</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="c1">// nan as u8 is 0</span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"nan as u8 is {}"</span><span class="p">,</span><span class="w"> </span><span class="kt">f32</span>::<span class="n">NAN</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u8</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="234541553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234541553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234541553">(Apr 14 2021 at 17:35)</a>:</h4>
<p>Okay, I see.</p>



<a name="234541629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234541629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234541629">(Apr 14 2021 at 17:35)</a>:</h4>
<p>it's saturating now, yes. used to be a soundness hole</p>



<a name="234541665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234541665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234541665">(Apr 14 2021 at 17:35)</a>:</h4>
<p>Is it truncating?</p>



<a name="234541738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234541738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234541738">(Apr 14 2021 at 17:36)</a>:</h4>
<p>believe so</p>



<a name="234541787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234541787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234541787">(Apr 14 2021 at 17:36)</a>:</h4>
<p>truncate then saturate</p>



<a name="234541881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234541881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234541881">(Apr 14 2021 at 17:37)</a>:</h4>
<p><a href="https://github.com/rust-lang/stdsimd/issues/38">https://github.com/rust-lang/stdsimd/issues/38</a></p>



<a name="234541902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234541902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234541902">(Apr 14 2021 at 17:37)</a>:</h4>
<p><span class="user-mention" data-user-id="281757">@Jubilee</span> have you seen this issue?</p>



<a name="234541929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234541929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234541929">(Apr 14 2021 at 17:37)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">1.6</span><span class="k">f32</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">i32</span><span class="p">);</span><span class="w"> </span><span class="c1">// -1</span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">1.1</span><span class="k">f32</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">i32</span><span class="p">);</span><span class="w"> </span><span class="c1">// -1</span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="mf">1.1</span><span class="k">f32</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">i32</span><span class="p">);</span><span class="w"> </span><span class="c1">// 1</span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="mf">1.6</span><span class="k">f32</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">i32</span><span class="p">);</span><span class="w"> </span><span class="c1">// 1</span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="mf">0.6</span><span class="k">f32</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">i32</span><span class="p">);</span><span class="w"> </span><span class="c1">// 0</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="234541955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234541955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234541955">(Apr 14 2021 at 17:37)</a>:</h4>
<p>yup! I have indeed seen that issue.</p>



<a name="234542248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234542248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234542248">(Apr 14 2021 at 17:39)</a>:</h4>
<p>Okay, just making sure. We already have the conversion so we just need the checked version</p>



<a name="234542538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234542538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234542538">(Apr 14 2021 at 17:41)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> Let me overload <code>as</code> <span aria-label="smiling imp" class="emoji emoji-1f608" role="img" title="smiling imp">:smiling_imp:</span></p>



<a name="234542927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234542927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234542927">(Apr 14 2021 at 17:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="312331">Caleb Zulawski</span> <a href="#narrow/stream/257879-project-portable-simd/topic/.22Missing.22.20simd.20intrinsics/near/234542248">said</a>:</p>
<blockquote>
<p>Okay, just making sure. We already have the conversion so we just need the checked version</p>
</blockquote>
<p>is <code>to_int</code> supposed to be equivalent to <code>as</code>?</p>



<a name="234543813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234543813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234543813">(Apr 14 2021 at 17:47)</a>:</h4>
<p>It's supposed to be equivalent to std::f32::to_int (without the trait magic for different integer sizes, though)</p>



<a name="234544009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234544009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234544009">(Apr 14 2021 at 17:48)</a>:</h4>
<p>All that means is we need to check that the value is representable as the integer before converting, and return None otherwise</p>



<a name="234544373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234544373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234544373">(Apr 14 2021 at 17:50)</a>:</h4>
<p><a href="https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.to_int">https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.to_int</a> que?</p>



<a name="234544731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/float%20%E2%AC%8C%20int%20conversions/near/234544731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/float.20.E2.AC.8C.20int.20conversions.html#234544731">(Apr 14 2021 at 17:52)</a>:</h4>
<p>Hmmm</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>